#!/usr/bin/env python
# -*- coding:utf-8 -*-

import math
from app import db
from controller.adminInfoController import AdminInfoController
from models.adminInfoModel import AdminInfo
from models.passageInfoModel import PassageInfo
from models.userInfoModel import UserInfo
from utils import commons
from utils.response_code import RET, error_map_EN
from utils.loggings import loggings

SecretKey = 'sadfjnklsahjfd1354asdas'
class AdminInfoService(AdminInfoController):
    @classmethod
    def joint_query(cls, **kwargs):
        try:
            filter_list = []
            filter_list.append(cls.IsDelete == 0)
            if kwargs.get('AutoID'):
                filter_list.append(cls.AutoID == kwargs.get('AutoID'))
            if kwargs.get('AdminUserID'):
                filter_list.append(cls.AdminUserID == kwargs.get('AdminUserID'))
            if kwargs.get('AdminPassword'):
                filter_list.append(cls.AdminPassword == kwargs.get('AdminPassword'))
            if kwargs.get('IsDelete'):
                filter_list.append(cls.IsDelete == kwargs.get('IsDelete'))
            if kwargs.get('CreateTime'):
                filter_list.append(cls.CreateTime == kwargs.get('CreateTime'))
            if kwargs.get('ModifyTime'):
                filter_list.append(cls.ModifyTime == kwargs.get('ModifyTime'))
          
            page = int(kwargs.get('Page', 1))
            size = int(kwargs.get('Size', 10))
            
            adminInfo_info = db.session.query(AdminInfo.AutoID, AdminInfo.AdminUserID, AdminInfo.AdminPassword, AdminInfo.IsDelete, AdminInfo.CreateTime, AdminInfo.ModifyTime).filter(*filter_list)
            
            count = adminInfo_info.count()
            pages = math.ceil(count / size)
            adminInfo_info = adminInfo_info.limit(size).offset((page - 1) * size).all()
            
            if not adminInfo_info:
                return {'code': RET.NODATA, 'message': error_map_EN[RET.NODATA], 'error': 'No data to update'}
    
            # 处理返回的数据
            results = commons.query_to_dict(adminInfo_info)
            return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'totalCount': count, 'totalPage': pages, 'data': results}
        
        except Exception as e:
            loggings.exception(1, e)
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'error': str(e)}
        finally:
            db.session.close()

    @classmethod
    def login(cls, **kwargs):
        try:
            filter_list = [cls.IsDelete == 0]
            if kwargs.get('AdminUserID'):
                filter_list.append(cls.AdminUserID == kwargs.get('AdminUserID'))
            else:
                return{'code': RET.LOGINERR, 'message': error_map_EN[RET.LOGINERR], 'error': 'username cant be empty'}

            if kwargs.get('SecretKey'):
                if kwargs.get('SecretKey') != SecretKey:
                    return {'code': RET.LOGINERR, 'message': error_map_EN[RET.LOGINERR], 'error': 'SecretKeyError'}



            adminInfo_info = db.session.query(AdminInfo.AutoID, AdminInfo.AdminUserID, AdminInfo.AdminPassword,
                                              AdminInfo.IsDelete, AdminInfo.CreateTime, AdminInfo.ModifyTime).filter(
                *filter_list)

            adminInfo_info = adminInfo_info.first()

            if not adminInfo_info:
                return {'code': RET.USERERR, 'message': error_map_EN[RET.USERERR], 'error': 'Not existed User'}

            if kwargs.get('AdminPassword'):
                if kwargs.get('AdminPassword') != adminInfo_info.AdminPassword:
                    return{'code': RET.PWDERR, 'message': error_map_EN[RET.PWDERR], 'error': 'Pwd error'}
            else:
                return{'code': RET.PWDERR, 'message': error_map_EN[RET.PWDERR], 'error': 'Pwd cant be empty'}
            # 处理返回的数据
            results = commons.query_to_dict(adminInfo_info)
            return {'code': RET.OK, 'message': error_map_EN[RET.OK],
                    'data': results}

        except Exception as e:
            loggings.exception(1, e)
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'error': str(e)}
        finally:
            db.session.close()

    @classmethod
    def getpassageinfo(cls, **kwargs):
        try:
            filter_list = [PassageInfo.IsDelete == 0, UserInfo.IsDelete == 0]
            page = int(kwargs.get('Page', 1))
            size = int(kwargs.get('Size', 10))

            accuseInfo_info = db.session.query(PassageInfo.PostAuthorID,PassageInfo.CreateTime,PassageInfo.PostContent,PassageInfo.AutoID.label('PassageID'),PassageInfo.PostTitle,PassageInfo.DeptType,PassageInfo.PostType, UserInfo.FaceImg, UserInfo.UserNickName).join(UserInfo,
                                                                                                          UserInfo.UserID == PassageInfo.PostAuthorID).filter(
                *filter_list).order_by(PassageInfo.CreateTime.desc())
            count = accuseInfo_info.count()
            pages = math.ceil(count / size)
            accuseInfo_info = accuseInfo_info.limit(size).offset((page - 1) * size).all()

            if not accuseInfo_info:
                return {'code': RET.NODATA, 'message': error_map_EN[RET.NODATA], 'error': 'No data to update'}

            # 处理返回的数据
            results = commons.query_to_dict(accuseInfo_info)
            return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'totalCount': count, 'totalPage': pages,
                    'data': results}

        except Exception as e:
            loggings.exception(1, e)
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'error': str(e)}
        finally:
            db.session.close()

